var imgBase = 'http://img2.mbanggo.com/';
var url = 'tview';
var page = {
	cp : 1,
	pp : 20,
	total : 0,
	sort : ''
};
var baseUrl = '';
function getUrl(url) {
	return baseUrl + url; 
}
function list(n) {
	n = n || 1;
	var _url = url + '?cp=' + n + '&pp=' + page.pp + '&sort=' + page.sort + '&keyword=' + encodeURI(encodeURI($('#keyword').val())) +
				'&stDate='+$('#bdate').val()+'&endDate='+$('#edate').val();
	page.cp = n;
	jQuery.ajax({
		url : _url,
		type : 'POST',
		success : function(json) {
			var h = '<table style="margin:auto;">' +
					'<tr style="background-color:#86C5E0;font-weight:bold;color:white;">' +
					'	<td class="td1" >关键字</td>' +
					'	<td class="td2" style="cursor:pointer;" onclick="sort(0);">未处理</td>' +
					'	<td class="td3" style="cursor:pointer;" onclick="sort(1);">正确</td>' +
					'	<td class="td4" style="cursor:pointer;" onclick="sort(2);">错误</td>' +
					'	<td class="td4" style="cursor:pointer;" onclick="sort(7);">总数</td>' +
					'	<td class="td5" style="cursor:pointer;" onclick="sort(6);">查准率（%）</td>' +
					'	<td class="td6" style="cursor:pointer;" onclick="sort(4);">添加日期</td>' +
					'	<td class="td7">操作</td>' +
					'</tr>';
			if(json != null || json.length > 2) {
				var jsonObj = eval(json)[0];
				var obj = jsonObj.rs;
				var len = obj.length;
				page.total = jsonObj.total;
				if(len > 0){
					for(var i = 0; i < len; i ++) {
						var o = obj[i];
						var un = o.unhandleNum || 0;
						var ok = o.effectiveNum || 0;
						var no = o.invainNum || 0;
						var rate = o.hitRate || 0.0;
						var totalNum = o.total || 0;
						rate = (rate*100).toFixed(2);
						var id = o.id;
						var bgcolor = '';
						if(i%3 == 2) {
							bgcolor = 'style="background-color:#eeeeee"';
						}
						h += '<tr id="'+id+'" '+bgcolor+'>' +
							'	<td class="td1"><a target="_blank" style="color:red;" href="tviewProcess?keyword='+id+'">' + o.key + '</a>&nbsp;</td>' +
							'	<td class="td2">' + un + '</td>' +
							'	<td class="td3">' + ok + '</td>' +
							'	<td class="td4">' + no + '</td>' +
							'	<td class="td4">' + totalNum + '</td>' +
							'	<td class="td5">' + rate + '</td>' +
							'	<td class="td6">' + o.formatDate + '</td>' +
							'	<td class="td7"><a href="javascript:void(0);" class="operateA" onclick="deleteKeys(\''+id+'\');">删除</a>&nbsp;&nbsp;<a style="font-size:10px;color:green;" href="tweightInfo?keyword='+id+'" target="_blank">信息</a></td>' +
							'</tr>';
					}
				}else{
					h += '<tr><td colspan="7">没有结果</td></tr>';
				}
			}else{
				h += '<tr><td colspan="7">没有结果</td></tr>';
			}
			h += '</table>'
			$('#resultDiv').html(h);
			$('#pageNum').html(getPage(page.cp, page.pp, page.total, 'list'));
		}
	});
}

function getPage(cp, pp, total, func) {
	if(total <= 0) {
		return '';
	}
	var h = '';
	var deltPage = 4;
	var totalPage = Math.ceil(total/pp);
	var firstPage = cp > 4 ? cp - 4 : 1;
	var maxPage = cp + 4 < totalPage ? cp + 4 : totalPage;
	var currentPage = cp;
	
	if(currentPage != 1) {
		h += '<span class="pageOff" onclick="'+ func +'(' + 1 +');">首页</span>';
	}
	for(var i = firstPage; i <= maxPage; i += 1) {
		if(i == currentPage){
			h += '<span class="pageOn" >' + i + '</span>';
		}else{
			h += '<span class="pageOff" onclick="'+ func +'(' + i +');">' + i + '</span>';
		}
	}
	
	if(totalPage > currentPage) {
		h += '<span class="pageOff" onclick="'+ func +'(' + totalPage +');">尾页</span>';
	}
	return h;
}

var SORT_BEANS = new Array();
function SortBean(index, status) {
	this.index = index || 0;
	this.status = status || 1;
}
SortBean.prototype.setStatus = function() {
	this.status = this.status*-1;
}
function initSortBean() {
	for(var i = 0; i <= 7; i += 1) {
		SORT_BEANS.push(new SortBean(i, 1));
	}
}
function sort(n) {
	n = n || 0;
	var obj = SORT_BEANS[n];
	if(n != null) {
		var s = obj.index + ':' + obj.status;
		obj.setStatus();
		page.sort = s;
		list();
	}
}


function deleteKeys(id) {
	if(!confirm("确定删除么？")) {
		return;
	}
	var ids = id || getIds();
	jQuery.ajax({
		url : 'tdeleteKey?keyIds=' + ids,
		type : 'POST',
		success : function(o) {
			if(o && o == 1) {
				var idarr = ids.split(',');
				for(var i = 0; i < idarr.length; i ++){
					$('#'+idarr[i]).fadeOut(200);
				}
			}
		}
	});
}
function getIds(){
	return '';
}
String.prototype.trim = function() {
	var val = this;
	return val.replace(/\s*/g, '');
}
